package com.kye.express.business.name_certification;

import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.ErrorView;
import com.kye.express.contract.base.view.IView;
import com.kye.express.contract.base.view.LoadingView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.datasource.OrderDataSource;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public interface NameCertificationContract {
    interface NameCertificationView extends IView, LoadingView, ErrorView {

        void onCertified();
    }

    class NameCertificationPresenter extends BasePresenter<NameCertificationView> {

        private final OrderDataSource mOrderDataSource;

        public NameCertificationPresenter() {
            mOrderDataSource = new OrderDataSource();
        }

        public void nameCertification(String name, String idType, String id) {
            mOrderDataSource.nameCertification(name, id, idType)
                    .compose(getView().bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<Object>() {
                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<Object> result, Object o) {
                            getView().onCertified();
                        }

                        @Override
                        public void onApiError(OpenApiResult<Object> result, int code, String msg) {
getView().onError(msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
getView().onError();
                        }

                        @Override
                        public void onLoaded() {
                            getView().onLoaded();
                        }
                    });
        }
    }
}
